



//
//  ZHomeSearchResultVC.m
//  ZQLearn
//
//  Created by macfeel on 2019/5/13.
//  Copyright © 2019 macfeel. All rights reserved.
#import "ZBookMsgDetailVC.h"
#import "ReadModel.h"
#import "ZHomeSearchResultVC.h"
#import "SearchBar.h"
#import "OrderCell.h"//借用订单cell
#import "HomeModel.h"
#import "ZHomeSearchSubVC.h"

@interface ZHomeSearchResultVC()
<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource,ZJScrollPageViewDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,assign)NSInteger currentPage;

@property (nonatomic,strong)SearchBar *searchBar;

/**
 页面分栏的标题
 */
@property (strong,nonatomic) NSArray<NSString *>*titles;

/**
 横向分页滑动组件
 */
@property (strong,nonatomic) ZJScrollPageView *scrollPageView;
@end

#define cellIdentifier @"order"

@implementation ZHomeSearchResultVC

#pragma mark - 添加搜索框
- (void)addSearchBar{
    SearchBar *bar = [[[NSBundle mainBundle]loadNibNamed:@"SearchBar" owner:self options:nil]lastObject];
    bar.frame = CGRectMake(60, 7, ScreenWidth-120, 30);
    bar.layer.cornerRadius = 15.f;
    bar.layer.masksToBounds = YES;
    bar.souImg.hidden = YES;
    bar.barBtn.hidden = YES;
    bar.textFd.hidden = false;
    bar.textFd.delegate = self;
    self.searchBar = bar;
    //    self.navigationItem.titleView = bar;
    [self.navigationController.navigationBar addSubview:bar];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.searchBar.hidden = false;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.searchBar.hidden = YES;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight-ScreenHeightFlex) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 105.f;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"OrderCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        WeakStament(ws);
        //        _tableView.mj_header = [CustomRefreshHeader headerWithRefreshingBlock:^{
        //            [ws refreshData];
        //        }];
        
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //            [ws loadMoreData];
        //        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    //背景蓝
    //    self.navigationController.navigationBar.barTintColor = NColorFrom16RGB(0x23b7e5);
//    [self.navigationItem setItemWithTitle:@"书籍视频搜索" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 45, 45) title:@"搜索" Image:@"" selectedIMG:@"" tartget:self action:@selector(sousuoAction)];
    self.dataArray = [NSMutableArray array];
    [self addSearchBar];
    ///添加滑动条
    [self addTab];
    ///添加的顺序要变一下，可能是数据回来后再生成
//    [self.view addSubview:self.tableView];
}


- (void)addTab{
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    //显示滚动条
    style.showLine = YES;
    // 颜色渐变
    style.gradualChangeTitleColor = YES;
    style.scrollTitle = YES;
    style.scrollLineColor = NColorFrom16RGB(0x23b7e5);
    style.selectedTitleColor = NColorFrom16RGB(0x23b7e5);
    style.segmentViewBounces = YES;
    style.titleFont = kFont15;
    style.adjustCoverOrLineWidth = YES;
    style.autoAdjustTitlesWidth = YES;
    style.segmentHeight = 45;
    self.titles = @[@"书籍",
                    @"视频"];
    // 初始化
    _scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 20.0) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    [self.view addSubview:_scrollPageView];
}

//- (void)loadData{
////   NSDictionary *dic = @{@"name":self.searchBar.textFd.text?:@"",@"pageIndex":@"1",@"pageSize":@"30",};
//    NSDictionary *dic = @{@"name":self.searchBar.textFd.text?:@"",@"pageIndex":@"1",@"pageSize":@"100",};
//    NSString *url = kHomeSearchBookUrl;
//    [ZNetworkService getRequestWithBaseUrl:url setParameters:dic success:^(id result) {
//        YYLog(@"%@",result);
//        if ([result[@"errorCode"]integerValue]==0) {
//
//            NSArray *arr = result[@"result"];
//            for (NSDictionary *dic in arr) {
//                ReadModel *model = [ReadModel mj_objectWithKeyValues:dic];
//                [self.dataArray addObject:model];
//            }
//            [self.tableView reloadData];
//            [self.searchBar.textFd resignFirstResponder];
//
//        }
//    } fail:^(NSError *error) {
//
//    } delegater:nil];
//}
//
/////
- (void)sousuoAction{
    ///搜索接口请求
//    [self loadData];
    YYLog(@"%@",self.searchBar.textFd.text);
    if (self.searchBar.textFd.text.length==0) {
        [AFToast showText:@"请输入要搜索的内容"];
        return;
    }
    [UserModel shareInstance].txtContent = self.searchBar.textFd.text;
    [[NSNotificationCenter defaultCenter]postNotificationName:@"sou" object:nil];
    
}

//
//
//#pragma mark - datasource
//
//
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return self.dataArray.count;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    
//        ReadModel *model = self.dataArray[indexPath.row];
//        OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
//        cell.numLab.hidden = YES;
//        cell.jifenLab.textColor = kDarkGrayColor;
//        cell.nameLab.text = model.name;
//        cell.jifenLab.text = model.content;
//        cell.topHeight.constant = 20.f;
//        cell.bottomHeight.constant = 30.f;
//        //    cell.numLab.text =[NSString stringWithFormat:@"X%@",subModel.commodityNumber];
//        [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kPicServerUrl,model.photo]] placeholderImage:nil];
//        return cell;
//
//}
//
//
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    ReadModel *hmodel = self.dataArray[indexPath.row];
//    ZBookMsgDetailVC *det = [ZBookMsgDetailVC new];
//    MidModel *model = [MidModel new];
//    model.name = hmodel.name;
//    model.picUrl = [NSString stringWithFormat:@"%@%@",kPicServerUrl,hmodel.photo];
//    det.MModel = model;
//    [self.navigationController pushViewController:det animated:YES];
//}



#pragma mark ZJPhotoScrollViewDelegate
- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    
//    NewsModel *model = self.dataArray[index];
    

        //    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
        ZHomeSearchSubVC *childVc = (ZHomeSearchSubVC *)reuseViewController;
        if (!childVc) {
            
            childVc = [[ZHomeSearchSubVC alloc] init];
//            if (self.dataArray.count>0) {
//                childVc.datas = self.dataArray;
//            }
            
        }
        return childVc;
   
}

- (BOOL)shouldAutomaticallyForwardAppearanceMethods{
    return NO;
}




@end
